package com.cwj.mansysback.servlet.impl;

import com.cwj.mansysback.entity.Account;
import com.cwj.mansysback.entity.LocalAccount;
import com.cwj.mansysback.service.api.AccountService;
import com.cwj.mansysback.service.api.StudentService;
import com.cwj.mansysback.service.api.TeacherService;
import com.cwj.mansysback.service.impl.AccountServiceImpl;
import com.cwj.mansysback.service.impl.StudentServiceImpl;
import com.cwj.mansysback.service.impl.TeacherServiceImpl;
import com.cwj.mansysback.servlet.filter.LockFilter;
import com.cwj.mansysback.servlet.util.MyUtils;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class GetAccountServlet extends HttpServlet {


    private static final AccountService accSer = new AccountServiceImpl();

    private static final TeacherService teaSer = new TeacherServiceImpl();

    private static final StudentService studSer = new StudentServiceImpl();

    static class GetAccStatement extends Statement{
        public Account acc;

        public GetAccStatement() {
        }

        public GetAccStatement(Integer statue, Account acc) {
            super(statue);
            this.acc = acc;
        }

        public Account getAcc() {
            return acc;
        }

        public void setAcc(Account acc) {
            this.acc = acc;
        }
    }


    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("******************** GetAccountServlet start ********************");


        LocalAccount loAcc = LockFilter.localAccount;

        String aid = loAcc.getAid();

        Integer rank = loAcc.getAccrank();

        Account acc =new Account();

        if (rank.equals(Account.RankName.ADMIN.getRank())) {

            acc = accSer.getAccount(aid,rank);


        } else if (rank.equals(Account.RankName.TEACHER.getRank())) {

            acc = GetAccountServlet.teaSer.getTeacher(aid);




        } else if (rank.equals(Account.RankName.STUDENT.getRank())) {
            acc = GetAccountServlet.studSer.selectStudent(aid);
        }


        acc.lastChange();


        GetAccStatement statePack =new GetAccStatement(0,acc);

        MyUtils.writeResponse(response,statePack);

        System.out.println("******************** GetAccountServlet end ********************");

    }


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            this.doGet(request,response);
//        System.out.println( request.getParameter(LockFilter.NOWLOACC));
    }
}
